package com.honey.compilation.sql.ddl;

import java.util.ArrayList;
import java.util.List;

import com.honey.compilation.sql.SqlColumn;
import com.honey.compilation.sql.SqlKeyWord;
import com.honey.compilation.sql.SqlScript;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.IndentSpace;
import com.honey.core.utils.StringUtility;

/**
 * 删除索引, 非SQL 92定义
 * @author Administrator
 *
 */
@Deprecated
public class DropIndex extends  Drop implements SqlScript {
	

	private List<String> fields ;
	
	private String tableName;
	
	public DropIndex(String tableName){
		this.tableName = tableName;
		fields = new ArrayList<String>();
	}

	public DropIndex(String tableName,String  ...fields){
		this(tableName);
		addField(fields);
	}
	
	public DropIndex(String tableName,SqlColumn  ...fields){
		this(tableName);
		addField(fields);
	}
	
	public void addField(String ...fields){
		for(String field : fields)
			if(StringUtility.stringHasValue(field))this.fields.add(field);
	}
	
	public void addField(SqlColumn ...fields){
		for(SqlColumn field : fields)
			if(field != null)this.fields.add(field.getName().getValue());
	}
	
	@Override
	public CharacterBuilder compiledContent(int indentLevel)throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder();
		boolean newLine = false;
		for(String field : fields ){
			if (newLine)IndentSpace.newLine(answer);else newLine = true;
			
			answer.append( super.compiledContent( indentLevel ) )
			.append( SqlKeyWord.INDEX )
			.append( field ).append(SqlKeyWord.OPERATION_SPACE)
			.append( SqlKeyWord.ON)
			.append( tableName )
			.append(SqlKeyWord.OPERATION_SEMICOLON)
			;
		}
		return answer;
	}
	
	public enum IndexType{
		
		/**
		 * 默认
		 */
		NORMAL("Normal"),
		
		/**
		 * 唯一索引
		 */
		UNIQUE("UNIQUE"),
		
		/**
		 * 全文检索索引
		 */
		FULLTEXT("FULLTEXT"),
		
		;
		
		private final String name ;
		
		private IndexType(String name){
			this.name =  name;
		}
		
		public String getName() {
			return name;
		}
		
		@Override
		public String toString() {
			return name + SqlKeyWord.OPERATION_SPACE ;
		}
	}
}
